package com.steven.download;

import android.os.Bundle;
import android.util.Log;

import com.steven.download.download.db.DownloadEntity;
import com.steven.download.download.db.room.AppDataBase;
import com.steven.download.utils.Utils;
import com.steven.download.widget.recyclerView.LinearLayoutItemDecoration;

import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

public class ListZipDownloadActivity extends AppCompatActivity {
    private RecyclerView rv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_download);
        rv = findViewById(R.id.rv);
        List<DownloadEntity> list = getIntent().getParcelableArrayListExtra("list");
        List<DownloadEntity> entities = AppDataBase.getInstance(Utils.getContext()).getDownloadDao().queryAll();

        if (list != null && list.size() > 0) {
            for (DownloadEntity d : list) {
                d.setDownloadStatus(DownloadStatus.IDLE);
                if (entities != null && entities.size() > 0) {
                    for (DownloadEntity downLoad : entities) {
                        if (d.getPackageUrl().equals(downLoad.getUrl())) {
                            d.setDownloadStatus(DownloadStatus.valueOf(downLoad.getStatus()));
                            break;
                        }
                    }
                }
            }
        }
        setupAdapter(list);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e("ddd ", "kkkkkkkkkkkkkkkkkkk：");
        if (BaseApplication.runBack) {
            BaseApplication.runBack = false;
            moveTaskToBack(true);
        }

    }

    private void setupAdapter(List<DownloadEntity> list) {
        ZipAdapter adapter = new ZipAdapter(this, list, R.layout.zip_item);
        rv.addItemDecoration(new LinearLayoutItemDecoration(this, R.drawable.linear_layout_item));
        rv.setAdapter(adapter);
    }
}
